package com.hthy.futures.ui.fragment;

import android.view.View;

import com.hthy.futures.R;
import com.hthy.futures.api.ST_TradeApi;
import com.hthy.futures.base.BaseFragment;
import com.hthy.futures.utils.ErrorUtils;
import com.hthy.futures.utils.LogUtils;
import com.hthy.futures.utils.ShaUtils;
import com.hthy.futures.utils.StringUtils;
import com.hthy.futures.view.EditTextWithDel2;
import com.vilyever.socketclient.SocketClient;

import net.hthy.bean.Trade;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 交易密码修改
 * Author:  Mr.bingley
 * Version:
 * Date:  2017/3/27
 */

public class TradePwdFragment extends BaseFragment {
    @Bind(R.id.et_buy_pwd)
    EditTextWithDel2 mEtBuyPwd;
    @Bind(R.id.et_newpwd)
    EditTextWithDel2 mEtNewpwd;
    @Bind(R.id.et_moneypwd_again)
    EditTextWithDel2 mEtMoneypwdAgain;
    private String mBuyPwd;
    private String mNewPwd;
    private String mPwdAgain;

    @Override
    protected int getLayoutId() {
        return R.layout.trade_update;
    }

    @Override
    protected void onResponse(SocketClient client, String responMes, byte[] subBytes) {
        super.onResponse(client, responMes, subBytes);
        LogUtils.e(responMes);
        try {
            Trade.ChangePasswordResponseMsg changePasswordResponseMsg = Trade.ChangePasswordResponseMsg.parseFrom(subBytes);
            int iErrorCode = changePasswordResponseMsg.getResponseMsg().getIErrorCode();
            if (0 == iErrorCode) {   // 0说明是正确返回的
                showToast(R.string.success_mess);
                getActivity().finish();
            } else {
                ErrorUtils.sendErrorMes(getActivity(), iErrorCode + "");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnClick({R.id.bt_post, R.id.bt_dismiss})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_post:
                if (checkValueValide()) {
                    return;
                }
                checkValueValide();
                socketClient.sendData(ST_TradeApi.updatePwd(0, ShaUtils.getSha1(mNewPwd), ShaUtils.getSha1(mBuyPwd)));
                break;
            case R.id.bt_dismiss:
                getActivity().finish();
                break;
        }
    }

    private boolean checkValueValide() {
        mBuyPwd = mEtBuyPwd.getText().toString().trim();
        mNewPwd = mEtNewpwd.getText().toString().trim();
        mPwdAgain = mEtMoneypwdAgain.getText().toString().trim();

        if (StringUtils.isEmpty(mBuyPwd)) {
            showToast(R.string.trade_pwd);
            return true;
        }
        if (StringUtils.isEmpty(mNewPwd)) {
            showToast(R.string.trade_new_pwd);
            return true;
        }
        if (StringUtils.isEmpty(mPwdAgain)) {
            showToast(R.string.trade_new_pwd_agin);
            return true;
        }

        if (!mNewPwd.equals(mPwdAgain)) {
            showToast(R.string.trade_pwd_error);
            return true;
        }
        return false;
    }
}
